package com.tc.juc.n4;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

/**
 * @Auther: tianchao
 * @Date: 2021/10/4 12:03
 * @Description:
 */
@Slf4j
public class TestThreadState {

    public static void main(String[] args) {
        //testRunableToWaiting01();
        //testWaitingToBlock();
        //testRunableToWaiting02();
        //testRunableTOWaiting03();

    }

    public static void testRunableTOWaiting03(){
        Thread t1 = new Thread(()->{
            log.info("运行");
            LockSupport.park();
            log.info("结束");
        },"t1");
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("t1 状态 wait----> {}",t1.getState());
        LockSupport.unpark(t1);
        log.info("t1 状态 wait----> {}",t1.getState());
    }

    public static void testRunableToWaiting02(){
        Thread t1 = new Thread(()->{
            while (true){
                log.info("运行");
            }
        },"t1");
        t1.start();
        try {
            t1.join();
            log.info("main结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void testWaitingToBlock(){
        Object lock = new Object();
        Thread t1 = new Thread(()->{
            synchronized (lock){
                try {
                    log.info("执行");
                    lock.wait();
                    log.info("等待结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t1");
        Thread t2 = new Thread(()->{
            synchronized (lock){
                try {
                    log.info("执行");
                    lock.wait();
                    log.info("等待结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t2");
        t1.start();
        t2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("唤醒其他线程");
        synchronized (lock){
            lock.notifyAll();
        }
        log.info("t1 state : {}",t1.getState());
        log.info("t1 state : {}",t2.getState());
        log.info("main结束");
    }

    /**
     * 调用 obj.notify() ， obj.notifyAll() ， t.interrupt() 时
     * 竞争锁成功，t 线程从 WAITING --> RUNNABLE
     *
     * 竞争锁失败，t 线程从 WAITING --> BLOCKED
     */
    public static void testRunableToWaiting01(){
        Object lock = new Object();
        Thread t = new Thread(()->{
            synchronized (lock){
                try {
                    lock.wait();
                    log.info("等待结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        log.info("state new...: {}",t.getState());
        t.start();
        log.info("state runable....: {}",t.getState());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("state wait....: {}",t.getState());
    }




}
